<?php
class Prof_QuestController extends Zend_Controller_Action{
	private $_auth;
	public function init(){
		$this->_auth = Zend_Auth::getInstance();
	}
	public function addAction(){
		$request = $this->getRequest();
		$form = new Prof_Form_Question();
		$id = (int) $this->_getParam('qcm', null);

		if($this->getRequest()->isPost()){
			if($form->isValid($request->getPost()) && $id>0){
				$values = $form->getValues();
				$db = new Application_Model_DbTable_Question();

				$reponses = explode(PHP_EOL, $values['text']);
				unset($values['text']);

				$values['id_qcm'] = $id;
				$num_quest = $values['num_quest'] = $db->nextID($id);

				$db->insert($values);

				$questDB = new Application_Model_DbTable_Reponse();

				foreach ($reponses as $key => $value) {
					$data['texte'] = $value;
					$data['id_qcm'] = $id;
					$data['num_quest'] = $num_quest;
					$data['num_rep'] = ($key + 1);

					$questDB->insert($data);
				}
				$this->_helper->redirector('view','qcm','prof',array('qcm'=>$id));
			}else{
				$this->view->form = $form;
				$errors = $form->getMessages();
				$messages = array();
				foreach ($errors as $err) {
					foreach($err as $mess){
						$messages[]=$mess;
					}
				}
				if($id<1) $message[] = 'Invalid quiz id';
				$this->view->errors = $messages;
			}
		}else{
			$this->view->form = $form;
		}
	}
	public function editAction(){
		$request = $this->getRequest();
		$form = new Prof_Form_SimpleQuest();
		$db = new Application_Model_DbTable_Question();
		$id_qcm = (int) $this->_getParam('qcm', null);
		$num_quest = (int) $this->_getParam('quest', null);

		if($this->getRequest()->isPost()){
			if($form->isValid($request->getPost())){
				$values = $form->getValues();

				$db->update($values, array('id_qcm = ?'=>$values['id_qcm'],'num_quest = ?'=>$values['num_quest']));
				$this->_helper->redirector('view','qcm','prof',array('qcm'=>$id_qcm));
			}else{
				$this->view->form = $form;
				$errors = $form->getMessages();
				$messages = array();
				foreach ($errors as $err) {
					foreach($err as $mess){
						$messages[]=$mess;
					}
				}
				$this->view->errors = $messages;
			}
		}else{
			if($id_qcm>0 && $num_quest>0){
				$form->populate(get_object_vars($db->find($id_qcm, $num_quest)));
				$this->view->form = $form;
			}else{
				$this->_helper->redirector('deny','error');
			}
		}
	}
	public function deleteAction(){
		$id_qcm = (int) $this->_getParam('qcm', null);
		$num_quest = (int) $this->_getParam('quest', null);
		if($id_qcm>0 && $num_quest>0){
			$repDB = new Application_Model_DbTable_Reponse();
			$repDB->delete(array('id_qcm = ?'=>$id_qcm,'num_quest = ?'=>$num_quest));
			
			$questDB = new Application_Model_DbTable_Question();
			$questDB->delete(array('id_qcm = ?'=>$id_qcm,'num_quest = ?'=>$num_quest));
		
			$this->_helper->redirector('view','qcm','prof',array('qcm'=>$id_qcm));
		}else{
			$this->_helper->redirector('deny','error');
		}
	}
	public function viewAction(){
		$dbQuest =  new Application_Model_DbTable_Question();
		$dbRep = new Application_Model_DbTable_Reponse();
		$id_qcm = (int) $this->_getParam('qcm', null);
		$num_quest = (int) $this->_getParam('quest', null);
		
		if($id_qcm>0 && $num_quest>0){
			$quest = $dbQuest->findWithReponses($id_qcm, $num_quest);
			$this->view->quest = $quest;
			$repArray = $quest->reponses;

			$page =  (int) $this->_getParam('page', 1);
			$paginator = Zend_Paginator::factory($repArray);
			$paginator	->setItemCountPerPage(10)
			->setCurrentPageNumber($page)
			->setPageRange(3);
			$this->view->entries = $paginator;
			
		}else{
			$this->_helper->redirector('deny','error');
		}
		
	}
}